// pages/lygPage/lygPage.js
var app = getApp();
const util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    httpApiImg: app.globalData.httpApiImg,
    userInfo: '',
    title2: '',
    multiArray: [['1室', '2室', '3室', '4室', '5室', '5室以上'], ['0厅', '1厅', '2厅', '3厅', '3厅以上'], ['0卫', '1卫', '2卫', '3卫', '3卫以上']],
    multiSchool: '',
    formlist: [
      { isrequire: true, value: '', label: '面积', valueType: 'digit', key: 'area', tips: '请填写面积', type: 'input', right: '平米' },
      { isrequire: true, value: '', label: '总价', valueType: 'digit', key: 'totalMoney', tips: '请填写总价', type: 'input', right: '万元' },
      { isrequire: true, value: '', label: '付税方式', valueType: 'text', key: 'payTaxWay', tips: '请选择付税方式', type: 'select', data: ['双方各自赋税', '房主净得价'] },
      { isrequire: false, value: '', label: '税费', valueType: 'digit', key: 'taxFee', tips: '请选择税费', type: 'select', data: ['无增值税', '无个税', '无增值税无个税'] },
      { isrequire: true, value: '', label: '所属楼层', valueType: 'number', key: 'floor', tips: '请选择所属楼层', type: 'select', data: [] },
      { isrequire: true, value: '', label: '总楼层', valueType: 'number', key: 'totalFloor', tips: '请选择总楼层', type: 'select', data: [] },
      { isrequire: true, value: '', label: '产权', valueType: 'text', key: 'propertyRight', tips: '请选择产权', type: 'select', data: ['产权房', '使用权房'] },
      { isrequire: true, value: '', label: '装修', valueType: 'text', key: 'decorate', tips: '请选择装修', type: 'select', data: ['毛坯', '简装', '精装', '豪华装'] },
      { isrequire: true, value: '', label: '朝向', valueType: 'text', key: 'toward', tips: '请选择朝向', type: 'select', data: ['南北通透', '南', '东', '西', '北'] },
      { isrequire: true, value: '', label: '年份', valueType: 'number', key: 'year', tips: '请填写年份', type: 'select', mode: 'date', fields: 'year' },
      { isrequire: true, value: '', label: '区域', valueType: 'text', key: 'region', tips: '请选择区域', type: 'select', data: ["海州区", "连云区", "赣榆区"] },
      { isrequire: false, value: '', label: '地铁', valueType: 'text', key: 'subway', tips: '请选择地铁', type: 'select', data: ["无"] },
      { isrequire: false, value: '', label: '学区', valueType: 'text', key: 'schoolDistrict', tips: '请选择学区', type: 'select', data: [] },

    ],
    formData: {
      city: '连云港',
      releaseUserId: '',	//发布人id		
      houseSourceTypeId: '',	//房源类型id	
      picture: '',	//	房源图片			
      houseType: '',	//	房屋类型（1：新房 2：二手房）		
      communityName: '',	//	小区名称			
      businessType: '',	//	商业类型			
      houseDoor: '',	//	楼牌门栋			
      room: '',	//	室			
      hall: '',	//	厅			
      toilet: '',	//	卫生间			
      area: '',	//	面积		
      canBeRegistered: '',	//	是否可注册			
      unitPrice: '',	//	每平方米价格			
      totalMoney: '',	//	总价			
      payTaxWay: '',	//	付税方式			
      parkingProperties: '',	//	车位属性			
      taxFee: '',	//	税费			
      floor: '',	//	所属楼层			
      totalFloor: '',	//	总楼层			
      villaType: '',	//	别墅类型			
      propertyRight: '',	//	产权			
      decorate: '',	//	装修			
      currentSituation: '',	//	现状			
      toward: '',	//	朝向			
      year: '',	//	年份			
      maintenanceFee: '',	//	物业费			
      region: '',	//	地区			
      subway: '',	//	地铁			
      contact: '',	//	联系人			
      schoolDistrict: '',	//	学区			
      phone: '',	//	手机号			
      title: '',	//	标题			
      housingDescription: '',	//	房源描述			
      housingCharacteristics: '',	//	房源特色			
      rentalSaleStatus: '', 	//	出售状态	
      openingTime: '' //开盘时间
    },
    show: {
      schoolDistrict: '',
      houseType: '',
      houseDoor1: '',
      houseDoor2: '',
      houseDoor3: '',
      room: '',
      hall: '',
      toilet: ''
    },
    imgList: [],
    keys: '',
    inputShow: '',
    value: '',
    currentInput: '',
    active: '',
    canSubmit: false,
    houseTypeList: [{ id: 1, typeName: '新房' }, { id: 2, typeName: '二手房' }],
    bussTypeList: ['住宅底商', '连家店'],
    city: ''
  },
  bindOpenChange: function (e) {
    this.setData({
      ['formData.openingTime']: e.detail.value
    })
  },
  bindInputFunc: function (e) {
    let key = e.currentTarget.dataset.key;
    this.data.formData[key] = e.detail.value;
    this.setData({ formData: this.data.formData });
    this.changeValue();
  },
  bindBussChange: function (e) {
    var selectedItem = this.data.bussTypeList[e.detail.value];
    this.setData({
      ['formData.businessType']: selectedItem
    })
    this.changeValue();
  },
  bindTypeChange: function (e) {
    var selectedItem = this.data.houseTypeList[e.detail.value];
    this.setData({
      ['show.houseType']: selectedItem.typeName,
      ['formData.houseType']: selectedItem.id
    })
    this.changeValue();
  },
  schoolChange: function (e) {
    this.setData({
      ['formData.schoolDistrict']: this.data.multiSchool[1][e.detail.value[1]]
    })
    this.data.multiSchool[1][e.detail.value[1]]
  },
  bindcolumnchange: function (e) {
    var arr = [];
    if (e.detail.column == 0) {
      if (this.data.city == "连云港") {
        if (e.detail.value == 0) {
          arr = ["解放路教育集团高新区校区", "解放路教育集团郁洲分校", "解放路教育集团本部", "新浦实验小学", "滨河小学", "师专二附小教育集团孔望山小学", "师专二附小教育集团本部", "连云港实验学校小学部", "师专一附小教育集团盐河校区", "师专一附小教育集团龙河校区", "凤凰学校（小学部）", "苍梧小学", "向阳小学", "建宁小学", "海宁小学", "建国路小学", "大庆路小学", "幸福路小学", "新海小学", "大村中心小学", "南城中心小学", "海州实验小学", "海州第二实验小学", "许庄小学", "宁海中心小学", "新浦中学", "海庆中学", "海宁中学", "新海实验中学教育集团延安校区", "新海实验中学教育集团苍梧校区", "外国语学校", "凤凰学校（中学部）", "连云港实验学校中学部", "花果山中学", "海州实验中学", "宁海中学"]
        }
        else if (e.detail.value == 1) {
          arr = ["墟沟小学", "东港小学", "院前小学", "南巷小学", "海州湾小学", "陶庵小学", "庙岭小学", "临海路小学", "连岛小学", "云山小学", "白果树小学", "宿城小学", "板桥小学", "东港中学", "西苑中学", "新港中学", "连云中学", "板桥中学"]
        }
        else if (e.detail.value == 2) {
          arr = ["实验小学", "青口中心小学（新青小）", "青口小学（老青小）", "宁海路小学", "小荒小学", "大朱旭小学", "黄海路小学", "新城实验小学", "实验中学", "外国语中学"]
        }
      }
      this.data.multiSchool.splice(1, 1, arr);
      this.setData({
        multiSchool: this.data.multiSchool
      })
    }
  },
  otherInput: function (e) {
    var key = e.currentTarget.dataset.key;
    this.setData({
      [key]: e.detail.value
    })
    this.changeValue();
  },
  // 户型
  bindMultiPickerChange: function (e) {
    var arr = e.detail.value
    this.setData({
      ['formData.room']: this.data.multiArray[0][arr[0]].split("室")[0],
      ['show.room']: this.data.multiArray[0][arr[0]],
      ['formData.hall']: this.data.multiArray[1][arr[1]].split("厅")[0],
      ['show.hall']: this.data.multiArray[1][arr[1]],
      ['formData.toilet']: this.data.multiArray[2][arr[2]].split("卫")[0],
      ['show.toilet']: this.data.multiArray[2][arr[2]]
    })
    if (this.data.multiArray[0][arr[0]].indexOf("以上") > 0) {
      this.setData({
        ['formData.room']: this.data.formData.room + 1
      })
    }
    if (this.data.multiArray[1][arr[1]].indexOf("以上") > 0) {
      this.setData({
        ['formData.hall']: this.data.formData.hall + 1
      })
    }
    if (this.data.multiArray[2][arr[2]].indexOf("以上") > 0) {
      this.setData({
        ['formData.toilet']: this.data.formData.toilet + 1
      })
    }
    this.changeValue();
  },
  // 添加照片
  addPhoto: function () {
    wx: wx.navigateTo({
      url: '/pages/addPhoto/addPhoto'
    })
  },
  bindFormSubmit: function (e) {
    var item = this.data.currentInput;
    var flag = true;
    if (this.data.active != '') {
      if (item.reg) {
        var reg = new RegExp(item.reg);
        if (!reg.test(e.detail.value.con)) {
          flag = false;
          this.setData({
            'modelData.showModal': true,
            'modelData.content': '请输入正确的' + item.label
          })
        }
      }
      if (flag) {
        this.data.inputShow = false;
        this.data.formData[item.key] = e.detail.value.con;
        wx.setNavigationBarTitle({
          title: '入驻商家'
        })
        this.setData({
          inputShow: this.data.inputShow,
          value: '',
          formData: this.data.formData
        })
      }
    }
  },

  changeValue: function () {
    var flag = true;
    var item = this.data.formData;
    if (this.data.title2 == '住宅') {
      if ((item.houseSourceTypeId == '') || (item.communityName == '') || (item.room == '') || (item.area == '') || (item.totalMoney == '') || (item.payTaxWay == '') || (item.floor == '') || (item.totalFloor == '') || (item.propertyRight == '') || (item.decorate == '') || (item.toward == '') || (item.year == '') || (item.region == '')) {
        flag = false;
      }
    }
    if (this.data.title2 == '别墅') {
      if ((item.houseSourceTypeId == '') || (item.communityName == '') || (item.room == '') || (item.area == '') || (item.totalMoney == '') || (item.payTaxWay == '') || (item.floor == '') || (item.totalFloor == '') || (item.villaType == '') || (item.propertyRight == '') || (item.decorate == '') || (item.toward == '') || (item.year == '') || (item.region == '')) {
        flag = false;
      }
    }
    if (this.data.title2 == '写字楼') {
      if ((item.houseSourceTypeId == '') || (item.communityName == '') || (item.area == '') || (item.totalMoney == '') || (item.payTaxWay == '') || (item.floor == '') || (item.totalFloor == '') || (item.decorate == '') || (item.year == '') || (item.maintenanceFee == '') || (item.region == '')) {
        flag = false;
      }
    }
    if (this.data.title2 == '商铺') {
      if ((item.houseSourceTypeId == '') || (item.communityName == '') || (item.businessType == '') || (item.area == '') || (item.totalMoney == '') || (item.payTaxWay == '') || (item.floor == '') || (item.totalFloor == '') || (item.decorate == '') || (item.currentSituation == '') || (item.year == '') || (item.maintenanceFee == '') || (item.region == '')) {
        flag = false;
      }
    }
    if (this.data.title2 == '厂房仓库') {
      if ((item.houseSourceTypeId == '') || (item.communityName == '') || (item.area == '') || (item.totalMoney == '') || (item.payTaxWay == '') || (item.floor == '') || (item.totalFloor == '') || (item.region == '')) {
        flag = false;
      }
    }
    if (this.data.title2 == '车库车位') {
      if ((item.houseSourceTypeId == '') || (item.communityName == '') || (item.area == '') || (item.totalMoney == '') || (item.payTaxWay == '') || (item.parkingProperties == '') || (item.propertyRight == '') || (item.region == '')) {
        flag = false;
      }
    }
    this.setData({
      canSubmit: flag
    })
  },
  bindPickerChange: function (e) {
    var item = e.target.dataset.item;
    if (e.target.dataset.type != undefined) {
      if (item.mode == 'region') {
        this.data.formData[item.key] = e.detail.value[0] + e.detail.value[1] + e.detail.value[2]
      }
      else {
        this.data.formData[item.key] = e.detail.value;
      }
    }
    else {
      this.data.formData[item.key] = this.data.formlist[e.target.dataset.index].data[e.detail.value];
      if (item.label == '所属楼层') {
        var arr = [];
        for (var i = this.data.formData[item.key]; i < 101; i++) {
          arr.push(i)
        }
        if (this.data.title2 == '住宅' || this.data.title2 == '别墅' || this.data.title2 == '写字楼') {
          this.bindobj('formlist', arr, 'data', 5);
        }
        else if (this.data.title2 == '商铺' || this.data.title2 == '厂房仓库') {
          this.bindobj('formlist', arr, 'data', 4);
        }
      }
    }

    this.setData({
      formData: this.data.formData
    });
    this.changeValue();
  },

  // 下一步
  nextStep: function () {
    if (this.data.canSubmit) {
      if (this.data.show.houseDoor1) {
        this.data.formData.houseDoor = this.data.show.houseDoor1 + '栋/号楼' + this.data.show.houseDoor2 + '单元' + this.data.show.houseDoor3 + '室';
      }
      this.data.formData.unitPrice = (this.data.formData.totalMoney / this.data.formData.area * 10000).toFixed(2);
      this.data.formData.picture = this.data.imgList.join(",");
      wx.setStorageSync('house', this.data.formData);
      wx.navigateTo({
        url: '/pages/houseNext/houseNext?title2=' + this.data.title2
      })
    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var key1 = 'formData.releaseUserId';

    this.setData({
      city: wx.getStorageSync('city'),
      ['formData.houseSourceTypeId']: options.houseSourceTypeId,
      title2: options.title,
      [key1]: JSON.parse(wx.getStorageSync('userInfo')).id,
      userInfo: JSON.parse(wx.getStorageSync('userInfo'))
    })
    if (this.data.city == '连云港') {
      this.setData({
        multiSchool: [['海州区', '连云区', '赣榆区'], ["解放路教育集团高新区校区", "解放路教育集团郁洲分校", "解放路教育集团本部", "新浦实验小学", "滨河小学", "师专二附小教育集团孔望山小学", "师专二附小教育集团本部", "连云港实验学校小学部", "师专一附小教育集团盐河校区", "师专一附小教育集团龙河校区", "凤凰学校（小学部）", "苍梧小学", "向阳小学", "建宁小学", "海宁小学", "建国路小学", "大庆路小学", "幸福路小学", "新海小学", "大村中心小学", "南城中心小学", "海州实验小学", "海州第二实验小学", "许庄小学", "宁海中心小学", "新浦中学", "海庆中学", "海宁中学", "新海实验中学教育集团延安校区", "新海实验中学教育集团苍梧校区", "外国语学校", "凤凰学校（中学部）", "连云港实验学校中学部", "花果山中学", "海州实验中学", "宁海中学"]]
      })
    }
    wx.setNavigationBarTitle({
      title: options.title
    })
    if (this.data.title2 == '别墅') {
      var obj = { isrequire: true, value: '', label: '别墅', valueType: 'text', key: 'villaType', tips: '请选择别墅', type: 'select', data: ['独栋别墅', '联排别墅'] };
      this.data.formlist.splice(6, 0, obj);
    }
    else if (this.data.title2 == '写字楼') {
      var obj = { isrequire: false, value: '', label: '可注册', valueType: 'text', key: 'canBeRegistered', tips: '请选择可注册', type: 'select', data: ['可注册公司', '不可注册公司'] };
      this.data.formlist.splice(1, 0, obj);
      this.data.formlist.splice(4, 1);
      this.data.formlist.splice(6, 1);
      this.data.formlist.splice(7, 1);
      var obj1 = { isrequire: true, value: '', label: '物业费', valueType: 'text', key: 'maintenanceFee', tips: '请填写物业费', type: 'input', right: '元/月*平方米', largeright: true };
      this.data.formlist.splice(8, 0, obj1);
    }
    else if (this.data.title2 == '商铺') {
      this.data.formlist.splice(3, 1);
      this.data.formlist.splice(5, 1);
      this.data.formlist.splice(6, 1);
      var obj = { isrequire: true, value: '', label: '现状', valueType: 'text', key: 'currentSituation', tips: '请选择现状', type: 'select', data: ['营业中', '空铺'] };
      this.data.formlist.splice(6, 0, obj);
      var obj1 = { isrequire: true, value: '', label: '物业费', valueType: 'text', key: 'maintenanceFee', tips: '请填写物业费', type: 'input', right: '元/月*平方米', largeright: true };
      this.data.formlist.splice(8, 0, obj1);
    }
    else if (this.data.title2 == '厂房仓库') {
      this.data.formlist.splice(3, 1);
      this.data.formlist.splice(5, 4);
      // this.data.formlist.splice(4, 3);
    }
    else if (this.data.title2 == '车库车位') {
      this.data.formlist.splice(3, 3);
      var obj = { isrequire: true, value: '', label: '车位属性', valueType: 'text', key: 'parkingProperties', tips: '请选择车位属性', type: 'select', data: ['车库', '车位'] };
      this.data.formlist.splice(3, 0, obj);
      this.data.formlist.splice(5, 3);
    }
    this.setData({
      formlist: this.data.formlist
    })
    //wx.clearStorage();
  },
  bindobj: function (oldval, newval, key, index) {
    var arr = [];
    arr = this.data[oldval];
    arr[index][key] = newval;
    this.setData({
      formlist: arr
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var arr = [];
    for (var i = 1; i < 101; i++) {
      arr.push(i)
    }
    if (this.data.title2 == '住宅' || this.data.title2 == '别墅' || this.data.title2 == '写字楼') {
      this.bindobj('formlist', arr, 'data', 4);
      this.bindobj('formlist', arr, 'data', 5);
    }
    else if (this.data.title2 == '商铺' || this.data.title2 == '厂房仓库') {
      this.bindobj('formlist', arr, 'data', 3);
      this.bindobj('formlist', arr, 'data', 4);
    }
    //wx.clearStorage();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var img = wx.getStorageSync('img');
    if (img) {
      this.setData({
        imgList: img.split(",")
      })
    }
    else {
      this.setData({
        imgList: ''
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})